﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using ThreeByte.ActiveDeck.Config;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media;

namespace ThreeByte.ActiveDeck.Converters
{
    public class CategoryToColorMultiConverter : DependencyObject, IMultiValueConverter
    {
        public static readonly DependencyProperty AvailableColorsProperty = DependencyProperty.Register("AvailableColors",
                    typeof(Color[]), typeof(CategoryToColorMultiConverter), new PropertyMetadata(new Color[0]));

        public Color[] AvailableColors {
            get {
                return (Color[])GetValue(AvailableColorsProperty);
            }
            set {
                SetValue(AvailableColorsProperty, value);
            }
        }

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {

            try {
                Category c = (Category)(values[0]);
                ObservableCollection<Category> cats = (ObservableCollection<Category>)(values[1]);

                for(int i = 0; i < cats.Count; i++) {
                    if(c == cats[i]) {
                        return AvailableColors[i % AvailableColors.Length];
                    }
                }
            } catch {
                //Don't throw in a converter because it messes with the design-time editor
            }
            return null;
        }

        public object[] ConvertBack(object value, Type[] targetType, object parameter, System.Globalization.CultureInfo culture) {
            throw new Exception("Cannot convert back from CategoryToColorMultiConverter");
        }
    }
   
}
